package com.lyy.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyy.yygh.common.result.Result;
import com.lyy.yygh.common.utils.MD5;
import com.lyy.yygh.hosp.service.HospitalSetService;
import com.lyy.yygh.model.hosp.HospitalSet;
import com.lyy.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //查出所有的信息
    @ApiOperation(value = "所有医院设置信息")
    @GetMapping("findAll")
    public Result findAllhospital() {
        List<HospitalSet> list = hospitalSetService.list();
//        Result<List<HospitalSet>> ok = Result.ok(list);
        return Result.ok(list);
    }

    //删除医院
    @ApiOperation(value = "逻辑删除")
    @DeleteMapping("{id}")
    public Result remove(@PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    //条件查询带分页(创建vo类，封装条件值),@RequestBody表示用json数据传，required = false)表示可以是是空值,需要用postmapping
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页和每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        //构造条件,需要从vo中取出，进行判断
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String name = hospitalSetQueryVo.getHosname();
        String code = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(code)) {
            queryWrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }


        Page<HospitalSet> page1 = hospitalSetService.page(page, queryWrapper);
        return Result.ok(page1);


    }


    //添加医院

    @PostMapping("savahospitalSet")
    public Result save(@RequestBody HospitalSet hospitalSet) {
        //设置状态，1是可以使用的，0是不能使用的
        hospitalSet.setStatus(1);
        //设置签名
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    //根据id获取医院设置
    @GetMapping("getHospSet/{id}")
    public Result gethospset(@PathVariable Long id) {
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);

    }

    //修改医院设置接口
    @PostMapping("updateHospSet")
    public Result update(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    //批量删除(泛型为什么要用Long
    @DeleteMapping("Batchremove")
    public Result batchremove(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();

    }

    //医院设置锁定和解锁
    @PutMapping("lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable Long id, @PathVariable Integer status) {
        //根据id查询医院设置信息，在进行修改状态
        HospitalSet ids = hospitalSetService.getById(id);
        ids.setStatus(status);
        hospitalSetService.updateById(ids);
        return Result.ok();


    }

    //发送签名秘钥
    @PutMapping("sendkey/{id}")
    public Result sendkey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        hospitalSetService.updateById(hospitalSet);
        //TODO  发送短信
        return Result.ok();

    }

}
